package com.javaeedev.lightweight.mvc.interceptor;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import com.google.inject.Singleton;
import com.javaeedev.lightweight.mvc.Action;
import com.javaeedev.lightweight.mvc.Interceptor;
import com.javaeedev.lightweight.mvc.InterceptorChain;
import com.javaeedev.lightweight.mvc.Order;

@Singleton
@Order(5)
public class TransactionInterceptor implements Interceptor {

    private final Log log = LogFactory.getLog(getClass());

    public void intercept(Action action, InterceptorChain chain) throws Exception {
        //sample of using transaction:
        log.info("Begin transaction.");
        //Transaction tx = transactionManager.beginTransaction();
        try {
        	// can do something here
        	// ...
        	// then call back
            chain.doInterceptor(action);
            log.info("Commit transaction.");
            //tx.commit();
        }
        catch(Exception e) {
            log.warn("Rollback transaction.", e);
            //tx.rollback();
            throw e;
        }
    }

}
